// @ts-ignore
// @ts-nocheck
/*
J:\_ALL\CODE\gitlab.leigod.top\defender\src\qylg\Hooks_win64\Hook_Epic_assist.cpp

90.6.7+g19ba721+chromium-90.0.4430.212

void CefFrameHostImpl::SendCode(
    bool is_javascript,
    const std::string& code,
    const std::string& script_url,
    int script_start_line,
    CefRefPtr<CefResponseManager::Handler> responseHandler) {

.text:00007FFEEC04A93E libcef.dll:$28AA93E #28A9D3E
void CefFrameHostImpl::SendJavaScript(const std::string& jsCode,
                                      const std::string& scriptUrl,
                                      int startLine) {
*/
let log = console.log

function 生成字符串_std_string(ptr, len): NativePointer {
  let ret = Memory.alloc(8*4);
  ret.writePointer(ptr);
  ret.add(8).writeU64(len);
  ret.add(0x10).writeU64(len);
  ret.add(0x14).writeU32(0x80000000);
  return ret;
}

function 生成字符串_std_string_null(): NativePointer {
  let ret = 生成字符串_std_string(ptr(0), 0);
  // 长度为0时，不能有0x80000000标记。
  ret.add(0x14).writeU32(0);
  return ret;
}

function 生成全局js() {
  let _js = "{let msg = '2024-10-05, khz...'; console.log(msg); alert(msg);}";
  // let _js = "console.log( '2024-07-21 khz xyz000' );";
  // let _js = `document.querySelector('button[type="submit"]').style.cssText='pointer-events:none';document.querySelector('input[type="text"]').value='abc=====';`;
  let js = Memory.allocUtf8String(_js);
  global.js = js;
  global.jsCode = 生成字符串_std_string(js, _js.length);
  global.scriptUrl = 生成字符串_std_string_null();
  console.log('global.js ', global.js, global.jsCode, global.scriptUrl)
  log('js', js)
  return js
}

function test_js(ptr_exec_js, ecx) {
  // return;
  生成全局js()
  // ecx = new NativePointer("0x000029040030F260");

  let func = new NativeFunction(ptr_exec_js, 'void', ['pointer', 'pointer', 'pointer', 'int'], {abi: 'win64'})
  // let jsCode = ptr('0x00007FF8210C6F6C'); // Memory.allocUtf16String("alert( 'window_gotfocus' ); ");
  // let scriptUrl = ptr('0x00007FF8210C6F6C'); // Memory.allocUtf8String("alert( 'window_gotfocus' );");
  // log('jsCode, scriptUrl =  ', jsCode, scriptUrl)
  log('', typeof ecx, typeof ptr(ecx), typeof global.jsCode, typeof global.scriptUrl, typeof 0)
  func(ecx, global.jsCode, global.scriptUrl, 0);
}

function 遍历所有frame(m) {
  let addr_on_jsdialog = null;
  let exec_js_addr_call = 0x028AA93E;
  let exec_js_offset_info_manager_call = 0x033BDDCC;
  let exec_js_offset_info_list = 0x20;
  let exec_js_offset_info_list_size = 0x10;
  let exec_js_offset_begin = 0x10;
  let exec_js_offset_impl = 0xA8;
	// No host found for chrome popup browser
  // .text:00007FFEECB5DDCC libcef.dll:$33BDDCC #33BD1CC
  function GetCefBrowserInfoManager(addr_func)
	{
		// DWORD64(*func)();
    let func = new NativeFunction(addr_func, 'pointer', [], {abi: 'win64'});
		// MY_WRITE_DWORD64(&func, addr_func);
		return func();
	}

  let addr_func = m.add(exec_js_offset_info_manager_call);
  log('addr_func_info_manager', addr_func.toString(16))
  let obj_info_manager = GetCefBrowserInfoManager(addr_func)
  log('obj_info_manager', obj_info_manager.toString(16))
  let info_list = obj_info_manager.add(exec_js_offset_info_list)
  let info_list_size = info_list.add(exec_js_offset_info_list_size).readU32();
  log('info_list', info_list.toString(16), info_list_size);
  let iter_list = info_list.readPointer();
  for(let i=0; i<info_list_size; i++) {
    let ptrCefBrowserInfo = iter_list.add(exec_js_offset_begin).readPointer();
    let main_frame = ptrCefBrowserInfo.add(exec_js_offset_impl).readPointer();
    log('main_frame', main_frame.toString(16))
    test_js(m.add(exec_js_addr_call), main_frame)
    // break;

    iter_list = iter_list.readPointer();
  }
}

function main() {
  log('main')
  let m = Module.getBaseAddress('libcef.dll');
  遍历所有frame(m)
}

main();